/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: GetRequestBuilder.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.baselib.network.request;

import java.util.Iterator;

import io.bhex.baselib.network.Encode;
import io.bhex.baselib.network.params.GetParams;
import okhttp3.Request;


public class GetRequestBuilder implements IRequestBuilder<GetParams> {
    private GetParams params;

    @Override
    public IRequestBuilder params(GetParams params) {
        this.params = params;
        return this;
    }

    @Override
    public Request build() {

        Request.Builder builder = new Request.Builder()
                .url(params.getUrl())
                .tag(params.getTag())
                .get();

        if(params.getHeaders()!=null && params.getHeaders().size() > 0) {
            Iterator<String> keyIterator = params.getHeaders().keySet().iterator();

            while (keyIterator.hasNext()) {
                String key = keyIterator.next();

                builder.addHeader(key, Encode.urlEncode(params.getHeaders().get(key)));
            }
        }
        if (params.getCacheControl() != null)
            builder.cacheControl(params.getCacheControl());

        return builder.build();
    }
}
